from pynput.mouse import Button, Controller
import tkinter
import random
from pynput import keyboard
mouse = Controller()
keyboard = keyboard.Controller()


class HandAction():
    def __init__(self):
        # 获取屏幕宽高
        tk = tkinter.Tk()
        self.width = tk.winfo_screenwidth()
        self.height = tk.winfo_screenheight()
        tk.quit()
    # 左右手做不同的动作
    def action(self, type, lmList):
        if type == "Left":
            self.left(lmList)
        else:
            self.right(lmList)
    # 右手动作
    def right(self, lmList):
        if len(lmList) == 0:
            return
        # 握拳
        # 因为使用opencv的方向，高度是从左上角算的。左上角是0,0,这与现实世界是不同的
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] < lmList[14][1] and
                lmList[20][1] < lmList[18][1] and lmList[4][1] < lmList[2][1]):
            print("开火(右手开掌)")
        # mouse.press(Button.left)
        # 1指向右转
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            print("向上转(1指)")
            # 转动量
            x = random.randint(40, 70)
            mouse.move(0, x)
            if (mouse.position[1] < 0):
                mouse.position = (mouse.position[0],self.height)

        # 2指向左转
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            print("向下转(2指)")
            x = random.randint(40,70)
            mouse.move(0, -x)
            if (mouse.position[1] > self.height):
                mouse.position = (mouse.position[0],0)
    # 左手动作
    def left(self, lmList):
        if len(lmList) == 0:
            return
        # 1指向右转
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            print("向右转(1指)")
            # 转动量
            x = random.randint(100, 150)
            mouse.move(x, 0)
            if (mouse.position[0] > self.width):
                mouse.position = (0, mouse.position[1])

        # 2指向左转
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            print("向左转(2指)")
            x = random.randint(100, 150)
            mouse.move(-x, 0)
            if (mouse.position[0] < 0):
                mouse.position = (self.width, mouse.position[1])
        # # 开掌移动
        if (lmList[8][1] < lmList[6][1] and lmList[12][1] < lmList[10][1] and lmList[16][1] < lmList[14][1] and
                lmList[20][1] < lmList[18][1] and lmList[4][1] < lmList[2][1]):
            print("前进(开掌)")
            # keyboard.press('W')
        # # 握拳停止
        if (lmList[8][1] > lmList[6][1] and lmList[12][1] > lmList[10][1] and lmList[16][1] > lmList[14][1] and
                lmList[20][1] > lmList[18][1]):
            print("停止(握拳)")
            # keyboard.release('W')
